import axios from "axios";
// export const baseUrl = 'https://www.gps808.cn:8121';
// export const apiUrl = 'https://www.gps808.cn:8121/api';
export const baseUrl = 'http://110.40.142.133:8200';
export const apiUrl = 'http://110.40.142.133:8200/api';
export const webBaseUrl = 'http://www.gps808.cn/projector';
axios.interceptors.request.use(
  (config) => {
    const token = sessionStorage.getItem("token");

    if (token) {
      // 判断是否存在token，如果存在的话，则每个http header都加上token
      // console.log(token)
      config.headers.Authorization = "Bearer " + token;
    }
    //console.log('config', config);
    //const urlobj = JSON.parse(sessionStorage.getItem('baseurl') as string);
    config.url = apiUrl + config.url;
    return config;
  },
  (err) => {
    console.log("err-request", err);

    return Promise.reject(err);
  }
);

axios.interceptors.response.use(
    (config) => {
       // console.log('config',config)
    if (axios.isTransParms) {
      //是否返回接口整个返回参数
      return config.data;
    } else {
      const { Code, Message: msg,Status } = config.data;
      if (Status) {
        return config.data.Data;
      } else {

        //Toast.fail(msg);
        return Promise.reject(config.data?.Data);
      }
    }
  },
  (err) => {
    console.log("err", err.response);
    const { status } = err.response;
    if (status === 401) {
      sessionStorage.removeItem("token");
      //history.replace("/login");
    }
    return Promise.reject(err);
  }
);

const getAjaxFn = async (url, data = {}, isTransParms = false) => {
  axios.isTransParms = isTransParms;
  return axios.get(url, {
    params: {
      ...data,
    },
  });
};

const postAjaxFn = async (url, params = {}, isTransParms = false) => {
  axios.isTransParms = isTransParms;
  return axios.post(url, {
    ...params,
  });
};
if (!axios.getAjaxFn) {
  axios.getAjaxFn = getAjaxFn;
}
if (!axios.postAjaxFn) {
  axios.postAjaxFn = postAjaxFn;
}

export default axios;
